package com.gree.changying.widget.banner.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.gree.changying.R;
import com.gree.changying.utils.DisplayUtil;
import com.gree.changying.widget.banner.BannerLayout;
import com.gree.changying.widget.banner.bean.Photo;

import java.util.List;

/**
 * Create by 游德禄 on 2019-9-7
 */
public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.MzViewHolder> {

    private Context context;
    private List<Photo> urlList;
    private BannerLayout.OnBannerItemClickListener onBannerItemClickListener;

    private int width ;
    public BannerAdapter(Context context, List<Photo> urlList, int widht) {
        this.context = context;
        this.urlList = urlList;
        this.width = widht;
    }

    public void setOnBannerItemClickListener(BannerLayout.OnBannerItemClickListener onBannerItemClickListener) {
        this.onBannerItemClickListener = onBannerItemClickListener;
    }

    @Override
    public BannerAdapter.MzViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MzViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(BannerAdapter.MzViewHolder holder, final int position) {
        if (urlList == null || urlList.isEmpty())
            return;
        ViewGroup.LayoutParams params = holder.card.getLayoutParams();

        int width = this.width- DisplayUtil.dip2px(context,16f)*2;
        int height = 534*width/1331;
        params.width = width;
        params.height = height;
        holder.card.setLayoutParams(params);

        ViewGroup.LayoutParams g = holder.imageView.getLayoutParams();
        g.width = width;
        g.height = height;
        holder.imageView.setLayoutParams(g);
        //预留：对接后台再用这个方法
        //Glide.with(context).load(url).into(img);
       // Glide.with(context).load(R.drawable.main_header).into(img);
        Glide.with(context).load(urlList.get(position).getBg()).into(holder.imageView);

        holder.imageView.setTag(position);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBannerItemClickListener != null) {
                    int p = (int)v.getTag();
                    onBannerItemClickListener.onItemClick(p);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        if (urlList != null) {
            return urlList.size();
        }
        return 0;
    }


    class MzViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        CardView card;

        MzViewHolder(View itemView) {
            super(itemView);
            imageView =itemView.findViewById(R.id.image);
            card = itemView.findViewById(R.id.card);
        }
    }

}

